<?php
declare(strict_types=1);

namespace app\common\business;

use app\common\model\CommonBase as CommonBaseModel;
use app\common\model\ModuleField as ModuleFieldModel;

class ModuleField extends CommonBase
{
    private $_moduleFieldObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_websiteCode    = $websiteCode;
        $this->_moduleFieldObj = new ModuleFieldModel(['website_code'], $websiteCode);
    }

    public function getAllModuleField(array $where = []): array
    {
        $rows = [];
        try {
            $rows = $this->_moduleFieldObj->getAllModuleField($where);
        } catch (\Exception $e) {
            logErr('读取模型所有字段失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return $rows;
    }

    public function getModuleFieldList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $count = 0;
        $res   = [];
        try {
            $res   = $this->_moduleFieldObj->getModuleFieldList($page, $limit, $where);
            $count = $this->_moduleFieldObj->getModuleFieldCount($where);
        } catch (\Exception $e) {
            logErr('读取模型字段列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        $rows = $res->toArray();
        foreach ($rows as $key => $row) {
            $rows[$key]['status_name']     = $row['status'] == self::_IS_ENABLE_STATUS ? '启用' : '禁用';
            $rows[$key]['field_type_name'] = CommonBaseModel::FIELD_TYPE_NAME_MAP[$row['field_type']] ?? '';
            $rows[$key]['show_list_name']  = $row['show_list'] == CommonBaseModel::FIELD_IS_SHOW_LIST ? '显示' : '隐藏';
            $rows[$key]['is_require_name'] = $row['is_require'] == CommonBaseModel::FIELD_IS_REQUIRE ? '必填' : '选填';
            $rows[$key]['is_system_name']  = $row['is_system'] == CommonBaseModel::FIELD_IS_SYSTEM ? '系统' : '自定义';
        }

        return ['rows' => $rows, 'count' => $count];
    }

    public function addModuleField(array $data): array
    {
        try {
            unset($data['website_code']);
            $data['is_system'] = CommonBaseModel::FIELD_IS_CUSTOMER;
            return [self::_RESULT => $this->_moduleFieldObj->addModuleField($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加模型字段失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findModuleFieldById(int $id): array
    {
        $moduleField = null;
        $row         = null;
        try {
            $row = $this->_moduleFieldObj->findModuleFieldById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取模型字段详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($row)) {
            return [];
        }

        $moduleField                    = $row->toArray();
        $moduleField['status_name']     = $row['status'] == self::_IS_ENABLE_STATUS ? '启用' : '禁用';
        $moduleField['field_type_name'] = CommonBaseModel::FIELD_TYPE_NAME_MAP[$row['field_type']] ?? '';
        $moduleField['show_list_name']  = $row['show_list'] == CommonBaseModel::FIELD_IS_SHOW_LIST ? '显示' : '隐藏';
        $moduleField['is_require_name'] = $row['is_require'] == CommonBaseModel::FIELD_IS_REQUIRE ? '必填' : '选填';

        return $moduleField;
    }

    public function updateModuleField(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_moduleFieldObj->updateModuleField($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新模型字段信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function batchModuleFieldSort(array $data): array
    {
        try {
            return [self::_RESULT => $this->_moduleFieldObj->batchModuleFieldSort($data), self::_MESSAGE => self::_SORT_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('同主键排序模型字段失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteModuleField(int $id): array
    {
        try {
            return [self::_RESULT => $this->_moduleFieldObj->deleteModuleField($id), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除模型字段失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function clearModuleFieldCache(): bool
    {
        return $this->_moduleFieldObj->clearModuleFieldCache();
    }
}
